﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BlotterControl.cs" company="">
//   
// </copyright>
// <summary>
//   The blotter control.
// </summary>
// --------------------------------------------------------------------------------------------------------------------


using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using Systemathics.FrameWork;

namespace Systemathics.UserControls
{
    [ToolboxBitmap(typeof (BlotterControl), "UserControlsIcon.bmp")]
    public sealed partial class BlotterControl : UserControl
    {
        private readonly Color Grid_Color = Color.LightGray;
        private readonly Color Tick_Color = Color.White;
        private Blotter Blotter;
        private Instrument selectedInstrument;

        private readonly SynchronizationContext synchronizationContext;
        private readonly SendOrPostCallback UpdateBlotterDelegate;

        public Instrument SelectedInstrument
        {
            set
            {
                selectedInstrument = value;
                FilliT();
                BlingDGV.Invalidate();
            }
        }

        public BlotterControl()
        {
            InitializeComponent();
            Dock = DockStyle.Fill;
            DoubleBuffered = true;

            synchronizationContext = SynchronizationContext.Current;
            UpdateBlotterDelegate = new SendOrPostCallback(UpdateBlotter);

            BlingDGV.Rows.Add();
            BlingDGV.Rows.Add();
            BlingDGV.Rows.Add();
            BlingDGV.Rows.Add();
            BlingDGV.Rows.Add();

            BlingDGV.Columns[0].ValueType = typeof (int);
            BlingDGV.Columns[1].ValueType = typeof (double);
            BlingDGV.Columns[2].ValueType = typeof (double);
            BlingDGV.Columns[3].ValueType = typeof (int);

            BlingDGV.AutoResizeRows(DataGridViewAutoSizeRowsMode.DisplayedCells);
            BlingDGV.CellValueChanged += BlingDgvCellValueChanged;

        }

        private void FilliT()
        {
            Blotter = selectedInstrument.Blotter;
            lock (BlingDGV)
            {
                for (int i = 0; i < 5; i++)
                {
                    BlingDGV.Rows[i].Cells[0].Value = Blotter[(QuoteLevel)i].BidSize;
                    BlingDGV.Rows[i].Cells[1].Value = Blotter[(QuoteLevel)i].Bid;
                    BlingDGV.Rows[i].Cells[2].Value = Blotter[(QuoteLevel)i].Ask;
                    BlingDGV.Rows[i].Cells[3].Value = Blotter[(QuoteLevel)i].AskSize;
                }
            }
        }
        public new void Update()
        {
            synchronizationContext.Post(UpdateBlotterDelegate, null);
        }
        private void UpdateBlotter(Object obj)
        {
            Blotter = selectedInstrument.Blotter;
            lock (BlingDGV)
            {
                for (int i = 0; i < 5; i++ )
                {
                    if ((int)BlingDGV.Rows[i].Cells[0].Value != Blotter[(QuoteLevel)i].BidSize) 
                        BlingDGV.Rows[i].Cells[0].Value = Blotter[(QuoteLevel) i].BidSize;
                    if ((Decimal)BlingDGV.Rows[i].Cells[1].Value != Blotter[(QuoteLevel)i].Bid)
                        BlingDGV.Rows[i].Cells[1].Value = Blotter[(QuoteLevel)i].Bid;
                    if ((Decimal)BlingDGV.Rows[i].Cells[2].Value != Blotter[(QuoteLevel)i].Ask)
                        BlingDGV.Rows[i].Cells[2].Value = Blotter[(QuoteLevel)i].Ask;
                    if ((int)BlingDGV.Rows[i].Cells[3].Value != Blotter[(QuoteLevel)i].AskSize)
                        BlingDGV.Rows[i].Cells[3].Value = Blotter[(QuoteLevel)i].AskSize;
                }
            }
        }
        private void BlingDgvCellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewCell dgvc = BlingDGV[e.ColumnIndex, e.RowIndex];
            dgvc.Style.BackColor = (dgvc.Style.BackColor == Tick_Color) ? Grid_Color : Tick_Color;
            BlingDGV.InvalidateCell(dgvc);
        }
    }
}